import 'dart:async';

import 'package:bruno/bruno.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:pull_to_refresh_flutter3/pull_to_refresh_flutter3.dart';
import 'package:whale/pages/home/main/model/doc_sum.dart';
import '../../../helper/utils/util.dart';
import '../../app_tab/controller.dart';
import '../../means/model/means_model.dart';
import 'state.dart';

class HomeLogic extends GetxController with GetTickerProviderStateMixin {
  final HomeState state = HomeState();
  final tabState = Get.find<AppTabController>().state;
  @override
  void onInit() {
    state.rc = RefreshController();
    state.review_tabCtrl = TabController(length: 2, vsync: this);
    state.report_tabCtrl = TabController(length: 3, vsync: this);
    state.home_tabCtrl = TabController(length: 3, vsync: this);

    state.carouselData = [
      '河南省人民政府发布招标需求，评估为低风险',
      '北京市人民政府发布招标需求，评估为低风险',
      'XX市发布投标文件涉嫌违规，评估为高风险'
    ];

    initData();

    super.onInit();
    getHotData();


  }

  void onRefresh() {
    requestData();
  }

  void requestData() {}

  onScroll(bool b) {
    state.isShowTab = b;

    update();
  }

  @override
  void onReady() {
    super.onReady();
    initDy();
    requestData();
    var winedGuide = SpUtil().getBool(StorageKeys.firstGuide);

    if (winedGuide != true) {
      tabState.intro.start(Get.context!);

      SpUtil().setBool(StorageKeys.firstGuide, true);
    }

  }

  void initData() {
    getPageDoc();
    getPageSFDoc(type: '违规案例');
    getDocByTypeList();
    getNewDocSum();
  }

  //获取热搜人物
  void getHotData() async {
    state.hots.add('某区关于推动创业园发展的指导性意见1');
    state.hots.add('某区关于推动创业园发展的指导性意见2');
    state.hots.add('某区关于推动创业园发展的指导性意见3');
    state.hots.add('某区关于推动创业园发展的指导性意见4');
    state.hots.add('某区关于推动创业园发展的指导性意见5');
    state.hots.add('某区关于推动创业园发展的指导性意见6');
    state.hots.add('某区关于推动创业园发展的指导性意见7');

    update();
  }

  void showBottomView() {
    Get.bottomSheet(Container(
      height: 500.r,
      decoration: BoxDecoration(
          borderRadius: BorderRadius.vertical(top: Radius.circular(10.r)),
          color: Colors.white),
      child: Column(
        children: [
          SizedBox(
            height: 50.r,
            child: Stack(
              children: [
                Align(
                  alignment: Alignment.center,
                  child: Text(
                    '我要定制',
                    style: TextStyle(color: ColorsUtil.title, fontSize: 18.r),
                  ),
                ),
                Align(
                  alignment: Alignment.centerRight,
                  child: IconButton(
                    onPressed: () => Get.back(),
                    icon: Icon(
                      Icons.cancel_outlined,
                      color: ColorsUtil.subTitle,
                    ),
                  ),
                ),
              ],
            ),
          ),
          Divider(
            color: ColorsUtil.bg,
            thickness: 1.r,
            height: 1.r,
          ),
          Container(
            margin: EdgeInsets.all(10.r),
            child: Text(
              '为了满足更多客户在不同地域、不同行业领域的审查需求，观市平台可以依据客户的个性化需求，帮助客户完成符合需求的竞争审查。\n\n'
              '定制服务内容包含自定义数据来源、自定义指标库、个性化任务订阅XXXXXXXXXXXXXXXXXXXXXX、报告定制XXXXXXXXXXXXXXXXXXXXXXXXXXXXX等。\n\n'
              '如您有任何需求请随时与我们联系，也可以直接发邮件给我们，请在邮件中注明详细的联系方式。如公司名称、联系人、电话、电子邮件等。我们将及时与您联系，谢谢！\n\n'
              '服务热线：400-068-xxxx\n'
              '回电子邮件：service@topnet.com',
              style: TextStyle(color: ColorsUtil.subTitle, fontSize: 14.r),
            ),
          )
        ],
      ),
    ));
  }

  initDy() {
    if (state.offsetY1 == 0) {
      RenderBox renderBox =
          state.keyGreen1.currentContext!.findRenderObject() as RenderBox;

      Offset offset = renderBox.localToGlobal(Offset.zero);
      state.offsetY1 = offset.dy;
    }

    if (state.offsetY2 == 0) {
      RenderBox renderBox =
          state.keyGreen2.currentContext!.findRenderObject() as RenderBox;

      Offset offset = renderBox.localToGlobal(Offset.zero);
      state.offsetY2 = offset.dy;
    }
    if (state.offsetY3 == 0) {
      RenderBox renderBox =
          state.keyGreen3.currentContext!.findRenderObject() as RenderBox;

      Offset offset = renderBox.localToGlobal(Offset.zero);
      state.offsetY3 = offset.dy;
    }
  }


  void getPageDoc({String? type}) async{
    var res = await HomeApi.pageDoc( type: type??'政策措施');
    if(state.rc.isRefresh){
      state.rc.refreshCompleted();
    }
    if (res.isSuccess) {

      List records = res.data['records'];
      state.docZB = records.map((e) => MeansModel.fromJson(e)).toList();
      update();
    }
  }
  void getPageSFDoc({String? type}) async{
    var res = await HomeApi.pageDoc( type: type??'违规案例');
    if(state.rc.isRefresh){
      state.rc.refreshCompleted();
    }
    if (res.isSuccess) {

      List records = res.data['records'];
      state.docSF = records.map((e) => MeansModel.fromJson(e)).toList();
      update();
    }
  }

  void getDocByTypeList() async{
    var res = await HomeApi.pageDocByTypeList();
    if(state.rc.isRefresh){
      state.rc.refreshCompleted();
    }
    if (res.isSuccess) {
      List records = res.data['records'];
      state.docjx = records.map((e) => MeansModel.fromJson(e)).toList();

      update();
    }
  }

  void getNewDocSum({String? type}) async{
    var res = await HomeApi.getNewDocSum();
    if(state.rc.isRefresh){
      state.rc.refreshCompleted();
    }
    if (res.isSuccess) {
      state.docSum = DocSum.fromJson(res.data);


      update();
    }
  }

  scrollTo(int index) {
    if (index == 0) {
      state.scrollController.jumpTo(state.offsetY1);
    } else if (index == 1) {
      state.scrollController.jumpTo(state.offsetY2);
    } else if (index == 2) {
      state.scrollController.jumpTo(state.offsetY3);
    }
  }
}
